package com.aaa.inbound.controller;


import com.aaa.entity.inbound.Receive;
import com.aaa.entity.inbound.ReceiveDetail;
import com.aaa.inbound.service.ReceiveDetailService;
import com.aaa.inbound.service.ReceiveService;
import com.aaa.vo.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author aaa
 * @since 2020-11-25
 */
@RestController
@RequestMapping("/inbound/receive-detail")
@Api(description = "接货单详情管理")
public class ReceiveDetailController {
    @Autowired
    private ReceiveDetailService receiveDetailService;

    @Autowired
    private ReceiveService receiveService;

    @PostMapping("/saveReceive")
    @ApiOperation(value = "新增/修改接货商品方法")
    public R saveReceive(@RequestBody ReceiveDetail detail) {
        Long commodityid = detail.getCommodityid();
        Long pid = detail.getPid();
        QueryWrapper<ReceiveDetail> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pid", pid);
        queryWrapper.eq("commodityid", commodityid);
        ReceiveDetail one = receiveDetailService.getOne(queryWrapper);
        if (one!=null){
            one.setQty(detail.getQty()+one.getQty());
            boolean b = receiveDetailService.updateById(one);
            if (b) {
                return R.ok().code(200).message("修改成功");
            }
            return R.error().code(500).message("修改失败");
        }else {
            boolean b = receiveDetailService.saveDetail(detail);
            if (b) {
                return R.ok().code(200).message("添加成功");
            }
            return R.error().code(500).message("添加失败");
        }

    }

    @PostMapping("/selectDetail/{pid}")
    @ApiOperation(value = "根据接货单id查询所有商品")
    public R selectDetail(@ApiParam(value = "接货单id") @PathVariable Long pid) {
        try {
            QueryWrapper<ReceiveDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("pid", pid);
            List<ReceiveDetail> list = receiveDetailService.list(queryWrapper);
            return R.ok().code(200).data("list", list);
        } catch (Exception e) {
            return R.error().code(500).message("查询方法有误");
        }
    }

    @PostMapping("/selectDetailByNo/{no}")
    @ApiOperation(value = "根据接货单no查询所有商品")
    public R selectDetailByNo(@PathVariable String no) {
        try {
            QueryWrapper<Receive> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("receiveno", no);
            Receive one = receiveService.getOne(queryWrapper);

            QueryWrapper<ReceiveDetail> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.eq("pid", one.getId());
            List<ReceiveDetail> list = receiveDetailService.list(queryWrapper2);
            return R.ok().code(200).data("list", list);
        } catch (Exception e) {
            return R.error().code(500).message("查询方法有误");
        }
    }

}

